package modules

import (
	"crypto/tls"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"path"
	"time"

	"gopkg.in/go-module/log.v1"

	"gitee.com/Flearning/go_crawl/setting"
	"gitee.com/Flearning/go_crawl/utils"
)

type (
	ReqCrawlInfo struct {
		Method          string            `json:"method,omitempty"`
		Url             string            `json:"url,omitempty"`
		CrawlCollection string            `json:"crawlCollection,omitempty"`
		StripCollection string            `json:"stripCollection,omitempty"`
		Header          map[string]string `json:"req_header,omitempty"`
		Prames          []XueQiuPrames    `json:"prames,omitempty"`
	}

	XueQiuPrames struct {
		Symbol string `json:"symbol,omitempty"`
		Page   int    `json:"page,omitempty"`
		Sort   string `json:"sort,omitempty"`
		Sleep  int64  `json:"sleep,omitempty"`
		Source string `json:"source,omitempty"`
	}
)

func (r *ReqCrawlInfo) HttpNewRequest(urlStr string) (statusCode int, respBody []byte) {
	var err error
	var tr *http.Transport
	var client *http.Client

	ck := utils.CheckUrl(urlStr)
	if ck {
		tr = &http.Transport{
			MaxIdleConns:       10,
			IdleConnTimeout:    30 * time.Second,
			DisableCompression: true,
			TLSClientConfig:    &tls.Config{InsecureSkipVerify: true},
		}

		client = &http.Client{
			Transport: tr,
		}
	} else {
		client = &http.Client{}
	}

	req, _ := http.NewRequest(r.Method, urlStr, nil)

	for key, val := range r.Header {
		req.Header.Add(key, val)
	}

	resp, err := client.Do(req)
	if err != nil {
		log.Error(4, "httpNewRequest error: %v", err)
	}
	statusCode = resp.StatusCode
	if statusCode == http.StatusOK {
		respBody, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			log.Error(4, "httpNewRequest ReadAll error: %v", err)
		}
		return
	}
	return statusCode, nil
}

func (r *ReqCrawlInfo) New(rb []byte) *ReqCrawlInfo {
	reqInfo := new(ReqCrawlInfo)
	json.Unmarshal(rb, reqInfo)
	return reqInfo
}

func (r *ReqCrawlInfo) ReadCrawlInfoFile() ([]byte, error) {
	siteinfos := setting.Sites
	crawlInfoLen := len(siteinfos)
	log.Info("Crawl Site Config Count: %d", crawlInfoLen)

	pwd, _ := utils.WorkDir()
	cfgPath := path.Join(pwd, "conf", siteinfos[0])

	ckFile := utils.IsFile(cfgPath)

	if ckFile {
		rb, err := ioutil.ReadFile(cfgPath)
		if err != nil {
			log.Error(4, "ReadFile config `./conf/%s` Error: %v", siteinfos[0], err)
			return nil, err
		}
		return rb, err
	} else {
		err := fmt.Errorf("Can not Read config `./conf/%s`", siteinfos[0])
		log.Error(4, "Can not Read config `./conf/%s", siteinfos[0])
		return nil, err
	}
}
